#ifndef HTTPDATABASECLIENT_H
#define HTTPDATABASECLIENT_H

#include <QUrl>
#include "abstract-database-client.h"
#include "sm-global.h"

class QAbstractTableModel;
class QNetworkAccessManager;
class QNetworkReply;
class QNetworkRequest;

class HttpDatabaseClient : public AbstractDatabaseClient
{
    Q_OBJECT
public:
    enum DatabaseRequestType
    {
        ConnectToDatabaseRequest,
        SelectModelRequest,
        SelectExistsRequest,
        InsertDataRequest,
        UpdateDataRequest,
        RemoveDataRequest
    };

    explicit HttpDatabaseClient (QObject *parent = 0);

    ValueResult* connectToDatabase();
    ValueResult* disconnectFromDatabase();
    bool isConnected();
    ValueResult* selectExists (const QString& queryText, const QVariantList& parameters);
    ModelResult* selectModel (const QString& queryText, const QVariantList& parameters);
    ValueResult* insertData (const QString &queryText, const QVariantList &parameters);
    ValueResult* updateData (const QString &queryText, const QVariantList &parameters);
    ValueResult* removeData(const QString &queryText, const QVariantList &parameters);

public slots:
    void setHostName (QString hostName);
    void setPort (quint16 port);

private slots:
    void processReply (QNetworkReply* reply);
    void processConnectToDatabase (QNetworkReply* reply);
    void processSelectModel (QNetworkReply* reply);
    void processSelectExists (QNetworkReply *reply);
    void processInsertData(QNetworkReply *reply);
    void processUpdateData(QNetworkReply *reply);
    void processRemoveData(QNetworkReply *reply);

private:
    QString prepareQuery (QString queryText, QVariantList parameters);
    QNetworkRequest prepareRequest (DatabaseRequestType requestType);

    QNetworkAccessManager* m_networkAccessManager;
    QMap<QNetworkReply*,ValueResult*> m_valueResultForReply;
    QMap<QNetworkReply*,ModelResult*> m_modelResultForReply;
    QUrl m_url;
    QString m_invalidQueryError;
};

#endif // HTTPDATABASECLIENT_H
